#include "stdafx.h"
#include "Convert.h"


//////////////////////////////////////////////////////////////////////////////////////////////
//
void Convert::AStringToWString(const std::string& strSrc, std::wstring& strDest)
{
	wchar_t* pwbuf = NULL;
	int len = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, NULL, 0);
	pwbuf = new wchar_t[len + 1];
	memset(pwbuf, '\0', (len + 1)*sizeof(wchar_t));
	MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, pwbuf, len*sizeof(wchar_t));
	strDest = pwbuf;
	delete[] pwbuf;
	pwbuf = NULL;
}

void Convert::WStringToAString(const std::wstring& strSrc, std::string& strDest)
{
	int len = WideCharToMultiByte(CP_ACP, 0, strSrc.data(), -1, NULL, 0, NULL, NULL);
	char *pbuf = new char[len + 1];
	memset(pbuf, '\0', len + 1);
	WideCharToMultiByte(CP_ACP, 0, strSrc.data(), -1, pbuf, len, NULL, NULL);
	strDest = pbuf;
	delete[] pbuf;
	pbuf = NULL;
}

void Convert::WCharToAChar(wchar_t* pwszSrc, char* pszDest)
{
	int len = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
	char *pbuf = new char[len + 1];
	memset(pbuf, '\0', len + 1);
	WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pbuf, len, NULL, NULL);
	
	sprintf(pszDest, "%s", pbuf);

	delete[] pbuf;
	pbuf = NULL;
}

//void Convert::AStringToWString(const std::string& strSrc, CString& strDest)
//{
//	wchar_t* pwbuf = NULL;
//	int len = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, NULL, 0);
//	pwbuf = new wchar_t[len + 1];
//	memset(pwbuf, '\0', (len + 1)*sizeof(wchar_t));
//	MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, pwbuf, len*sizeof(wchar_t));
//	strDest.Format(_T("%s"), pwbuf);
//	delete[] pwbuf;
//	pwbuf = NULL;
//}
//
//void Convert::WStringToAString(const CString& strSrc, std::string& strDest)
//{
//	int len = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
//	char *pbuf = new char[len + 1];
//	memset(pbuf, '\0', len + 1);
//	WideCharToMultiByte(CP_ACP, 0, strSrc, -1, pbuf, len, NULL, NULL);
//	strDest = pbuf;
//	delete[] pbuf;
//	pbuf = NULL;
//}


//////////////////////////////////////////////////////////////////////////////////////
//
void Convert::Utf8ToGb2312(const std::string& strSrc, std::wstring& strDest)
{
	wchar_t* pwbuf = NULL;
	int len = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);
	pwbuf = new wchar_t[len + 1];
	memset(pwbuf, '\0', (len + 1)*sizeof(wchar_t));
	MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, pwbuf, len*sizeof(wchar_t));
	strDest = pwbuf;
	delete[] pwbuf;
	pwbuf = NULL;
}

void Convert::Utf8ToGb2312(const std::string& strSrc, std::string& strDest)
{
	std::wstring wtmp;
	Utf8ToGb2312(strSrc, wtmp);
	WStringToAString(wtmp, strDest);
}

void Convert::Gb2312ToUtf8(const std::wstring& strSrc, std::string& strDest)
{
	int len = WideCharToMultiByte(CP_UTF8, 0, strSrc.data(), -1, NULL, 0, NULL, NULL);
	char *pbuf = new char[len + 1];
	memset(pbuf, '\0', len + 1);
	WideCharToMultiByte(CP_UTF8, 0, strSrc.data(), -1, pbuf, len, NULL, NULL);
	strDest = pbuf;
	delete[] pbuf;
	pbuf = NULL;
}

void Convert::Gb2312ToUtf8(const std::string& strSrc, std::string& strDest)
{
	std::wstring wtmp;
	AStringToWString(strSrc, wtmp);
	Gb2312ToUtf8(wtmp, strDest);
}